package com.dianning.web.action;

import java.util.Date;
import java.util.List;

import org.apache.commons.logging.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.dianning.bean.AdAds;
import com.dianning.bean.Company;
import com.dianning.bean.Credit;
import com.dianning.bean.Info;
import com.dianning.bean.Money;
import com.dianning.bean.MoneyLog;
import com.dianning.bean.Profile;
import com.dianning.bean.Resume;
import com.dianning.bean.UserDetail;
import com.dianning.bean.UserInfo;
import com.dianning.bean.Visitor;
import com.dianning.comm.Constant;
import com.dianning.comm.Util;
import com.dianning.exception.DianningException;
import com.dianning.service.AdService;
import com.dianning.service.CreditService;
import com.dianning.service.InfoService;
import com.dianning.service.MoneyService;
import com.dianning.service.UserInfoService;
import com.dianning.service.VisitorService;
import com.dianning.web.interceptor.RemoteAddrAware;
import com.dianning.web.interceptor.UserSessionAware;
import com.dianning.web.servlet.UserSession;

public class GetPhone extends BaseAction implements UserSessionAware{

	private static final Log logger = LogFactory.getLog(GetPhone.class);

	private CreditService creditService;
	private MoneyService moneyService;
	private InfoService infoService;
	
	public CreditService getCreditService() {
		return creditService;
	}

	public void setCreditService(CreditService creditService) {
		this.creditService = creditService;
	}

	public MoneyService getMoneyService() {
		return moneyService;
	}
	
	public void setMoneyService(MoneyService moneyService) {
		this.moneyService = moneyService;
	}

	public InfoService getInfoService() {
		return infoService;
	}

	public void setInfoService(InfoService infoService) {
		this.infoService = infoService;
	}
	
	
	private UserSession userSession;

	public UserSession getUserSession() {
		return userSession;
	}

	public void setUserSession(UserSession userSession) {
		this.userSession = userSession;
	}
	
	private long id;		//主键ID
	private String callback;

	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	
	public String getCallback() {
		return callback;
	}

	public void setCallback(String callback) {
		this.callback = callback;
	}
	
	private String json;
	
	public String getJson() {
		return json;
	}

	public void setJson(String json) {
		this.json = json;
	}
	
	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return ERROR;
		}
	}
	public String index(){
		if(userSession.getUid()==0){
			JSONObject js = new JSONObject();
			try {
				js.put("free", "false");
				js.put("message", "登陆后查看");
			} catch (JSONException e) {
				logger.info(e);
			}
			if(this.getCallback() == null){
				this.setJson("{" + js.toString().substring(1, js.toString().length()-1) + "}");
			}else{
				this.setJson(this.getCallback() + "({" + js.toString().substring(1, js.toString().length()-1) + "})");
			}	
			return SUCCESS;
		}
		
		Credit c = this.getCreditService().findCreditByUidCode(userSession.getUid(),"get_phone");
		//无记录
		if(c==null){
			JSONObject js = new JSONObject();
			try {
				js.put("free", "true");
				js.put("message", "5");
			} catch (JSONException e) {
				logger.info(e);
			}
			if(this.getCallback() == null){
				this.setJson("{" + js.toString().substring(1, js.toString().length()-1) + "}");
			}else{
				this.setJson(this.getCallback() + "({" + js.toString().substring(1, js.toString().length()-1) + "})");
			}		
			return SUCCESS;
		}
		c = this.getCreditService().findCreditByUidCodeDate(userSession.getUid(),"get_phone");
		//免费
		if(c==null){
			JSONObject js = new JSONObject();
			try {
				js.put("free", "true");
				js.put("message", "5");
			} catch (JSONException e) {
				logger.info(e);
			}
			if(this.getCallback() == null){
				this.setJson("{" + js.toString().substring(1, js.toString().length()-1) + "}");
			}else{
				this.setJson(this.getCallback() + "({" + js.toString().substring(1, js.toString().length()-1) + "})");
			}
			return SUCCESS;
		}
		if(c.getNum()<5){
			JSONObject js = new JSONObject();
			try {
				js.put("free", "true");
				js.put("message", String.valueOf(5-c.getNum()));
			} catch (JSONException e) {
				logger.info(e);
			}
			if(this.getCallback() == null){
				this.setJson("{" + js.toString().substring(1, js.toString().length()-1) + "}");
			}else{
				this.setJson(this.getCallback() + "({" + js.toString().substring(1, js.toString().length()-1) + "})");
			}
			return SUCCESS;
		}
		JSONObject js = new JSONObject();
		try {
			js.put("free", "false");
			js.put("message", "查看(1积分)");
		} catch (JSONException e) {
			logger.info(e);
		}
		if(this.getCallback() == null){
			this.setJson("{" + js.toString().substring(1, js.toString().length()-1) + "}");
		}else{
			this.setJson(this.getCallback() + "({" + js.toString().substring(1, js.toString().length()-1) + "})");
		}
		return SUCCESS;
	}
	
	
	public String get(){
		if(userSession.getUid()==0){
			JSONObject js = new JSONObject();
			try {
				js.put("get", "false");
				js.put("message", "登陆后查看");
			} catch (JSONException e) {
				logger.info(e);
			}
			if(this.getCallback() == null){
				this.setJson("{" + js.toString().substring(1, js.toString().length()-1) + "}");
			}else{
				this.setJson(this.getCallback() + "({" + js.toString().substring(1, js.toString().length()-1) + "})");
			}	
			return SUCCESS;
		}
		Credit c = this.getCreditService().findCreditByUidCode(userSession.getUid(),"get_phone");
		//无记录
		if(c==null){
			c = new Credit();
			c.setUid(userSession.getUid());
			c.setCode("get_phone");
			c.setCycletype(1);
			c.setPoints(0);
			c.setNum(1);
			c.setAddTime(new Date());
			this.getCreditService().createCredit(c);
			Info info = this.getInfoService().findInfoById(this.id);
			JSONObject js = new JSONObject();
			try {
				js.put("tel", info.getTel());
				js.put("get", "true");
				js.put("message", "");
			} catch (JSONException e) {
				logger.info(e);
			}
			if(this.getCallback() == null){
				this.setJson("{" + js.toString().substring(1, js.toString().length()-1) + "}");
			}else{
				this.setJson(this.getCallback() + "({" + js.toString().substring(1, js.toString().length()-1) + "})");
			}
			logger.info(this.getJson());
			
			return SUCCESS;
		}
		c = this.getCreditService().findCreditByUidCodeDate(userSession.getUid(),"get_phone");
		//免费
		if(c==null){
			c = this.getCreditService().findCreditByUidCode(userSession.getUid(),"get_phone");
			c.setNum(1);
			c.setAddTime(new Date());
			this.getCreditService().createCredit(c);
			Info info = this.getInfoService().findInfoById(this.id);
			JSONObject js = new JSONObject();
			try {
				js.put("tel", info.getTel());
				js.put("get", "true");
				js.put("message", "");
			} catch (JSONException e) {
				logger.info(e);
			}
			if(this.getCallback() == null){
				this.setJson("{" + js.toString().substring(1, js.toString().length()-1) + "}");
			}else{
				this.setJson(this.getCallback() + "({" + js.toString().substring(1, js.toString().length()-1) + "})");
			}
			logger.info(this.getJson());
			return SUCCESS;
		}
		if(c.getNum()<5){
			c.setNum(c.getNum()+1);
			this.getCreditService().createCredit(c);
			Info info = this.getInfoService().findInfoById(this.id);
			JSONObject js = new JSONObject();
			try {
				js.put("tel", info.getTel());
				js.put("get", "true");
				js.put("message", "");
			} catch (JSONException e) {
				logger.info(e);
			}
			if(this.getCallback() == null){
				this.setJson("{" + js.toString().substring(1, js.toString().length()-1) + "}");
			}else{
				this.setJson(this.getCallback() + "({" + js.toString().substring(1, js.toString().length()-1) + "})");
			}
			logger.info(this.getJson());
			return SUCCESS;
		}
		Money m = this.getMoneyService().findMoneyByUid(userSession.getUid());
		if(m.getPoints()<1){
			JSONObject js = new JSONObject();
			try {
				js.put("get", "false");
				js.put("message", "您己经没有积分了");
			} catch (JSONException e) {
				logger.info(e);
			}
			if(this.getCallback() == null){
				this.setJson("{" + js.toString().substring(1, js.toString().length()-1) + "}");
			}else{
				this.setJson(this.getCallback() + "({" + js.toString().substring(1, js.toString().length()-1) + "})");
			}
			logger.info(this.getJson());
			return SUCCESS;
		}
		m.setPoints(m.getPoints() - 1);
		m.setExpend(m.getExpend() + 1);
		MoneyLog ml = new MoneyLog();
		ml.setUid(userSession.getUid());
		ml.setUsername(userSession.getUsername());
		ml.setLogtype("out");
		ml.setPoints(-1);
		ml.setExplain("查看联系方式 贴子编号为：" + this.id);
		ml.setAddTime(new Date());
		this.getMoneyService().updateMoney(m, ml);
		Info info = this.getInfoService().findInfoById(this.id);
		JSONObject js = new JSONObject();
		try {
			js.put("tel", info.getTel());
			js.put("get", "true");
			js.put("message", "");
		} catch (JSONException e) {
			logger.info(e);
		}
		if(this.getCallback() == null){
			this.setJson("{" + js.toString().substring(1, js.toString().length()-1) + "}");
		}else{
			this.setJson(this.getCallback() + "({" + js.toString().substring(1, js.toString().length()-1) + "})");
		}
		logger.info(this.getJson());
		return SUCCESS;
	}


}
